<?php

/**
* AdminEditSlideTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';
require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';
require_once '../framework/AuthSingleton.php';
require_once '../framework/Yt.class.php';


class  AdminEditSlideTest extends WebTestCase
{

    private $testName,$testDesc,$testGuideId,$url;
    private $username = '';
    private $password = '';

    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->testGeocode = md5(uniqid(rand(), true));
        $this->testNameNew = md5(uniqid(rand(), true));
        $this->testDescNew = md5(uniqid(rand(), true));
        $this->testGeocodeNew = md5(uniqid(rand(), true));        

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


        $dbh->exec("INSERT INTO guide (name,owner) values ( '".$this->testName."','".$this->username."' )");
        $this->testGuideId =  $dbh->lastInsertId();
        $dbh = null;

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);
        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->get($this->url.'?cmd=AdminAddSlide&guide_id='.$this->testGuideId);

    }

    public function testEditTextOnly()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    


        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testNameNew."' AND
                                                                   geocode = '".$this->testGeocodeNew."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   desc = '".$this->testDescNew."'");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

    }

    public function testEditTextAndPic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  pic_file = '".$this->testGuideId.$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_pic_file", 0 );
        $this->assertText($this->testGuideId.$lastInsertId.'images.jpg'); 
        $this->assertPattern('/send_image.php\?id='.$this->testGuideId.$lastInsertId.'images.jpg'.'/');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );

        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/imagesnew.jpg'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    
   

        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   geocode = '".$this->testGeocodeNew."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   pic_file = '".$this->testGuideId.$lastInsertId."imagesnew.jpg'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$this->testGuideId.$lastInsertId.'imagesnew.jpg'));

    }

    
    public function testEditTextAndVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();

        //Upload file to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.avi");
        $myVideoEntry->setVideoDescription("video.avi");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.avi");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.avi",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);
        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE slide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->assertFieldByName('yt_id',$matches[1]);
        $this->setFieldByName("delete_video_file", 0 );
        $this->assertText('Wideo w trakcie przetwarzania');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/videonew.avi'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    
   
        $this->assertTrue($this->setFieldByName('yt_id', '')); //clean old yt id

        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");
        
        $sth = $dbh->prepare("SELECT video_file FROM slide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   geocode = '".$this->testGeocodeNew."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   video_file NOT NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);

        $this->assertTrue(strpos($data,"Ten film wideo nie został jeszcze przetworzony."));
        $this->assertTrue(strpos($data,$this->testNameNew));
    
    }
   
    public function testEditTextAndSound()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');

 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  sound_file = '".$this->testGuideId.$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_sound_file", 0 );
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->assertLink('Pobierz dźwięk',$global['service_url'].'send_sound.php?id='.$this->testGuideId.$lastInsertId.'sound.wav');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/soundnew.wav'));
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));       
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    
   

        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   geocode = \"".$this->testGeocodeNew."\" AND 
                                                                   guide_id = \"".$this->testGuideId."\" AND
                                                                   sound_file = '".$this->testGuideId.$lastInsertId."soundnew.wav'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(is_file($upload['upload_dir'].$this->testGuideId.$lastInsertId.'soundnew.wav'));

    }


    public function testEditAll()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  sound_file = '".$this->testGuideId.$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav');

        //Upload video to YouTube
        //Upload file to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.avi");
        $myVideoEntry->setVideoDescription("video.avi");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.avi");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.avi",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);

        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE slide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        
        /////////////////////////


        $dbh->exec("UPDATE slide SET  pic_file = '".$this->testGuideId.$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_sound_file", 0 );
        $this->setFieldByName("delete_video_file", 0 ) ;  
        $this->setFieldByName("delete_pic_file", 0 ) ;
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->assertLink('Pobierz dźwięk',$global['service_url'].'send_sound.php?id='.$this->testGuideId.$lastInsertId.'sound.wav');
        $this->assertText('Wideo w trakcie przetwarzania');


        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/soundnew.wav'));
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/imagesnew.jpg'));
        $this->assertTrue($this->setFieldByName('video_file', dirname(__FILE__).'/videonew.avi'));
        $this->assertTrue($this->setFieldByName('desc', $this->testDescNew ));    
        $this->assertTrue($this->setFieldByName('geocode', $this->testGeocodeNew ));    
        $this->assertTrue($this->setFieldByName('name', $this->testNameNew ));    

        $this->assertTrue($this->setFieldByName('yt_id', ''));

        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID),video_file FROM slide WHERE name = '".$this->testNameNew."' AND 
                                                                   desc = '".$this->testDescNew."' AND 
                                                                   geocode = '".$this->testGeocodeNew."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   sound_file = '".$this->testGuideId.$lastInsertId."soundnew.wav' AND
                                                                   video_file NOT NULL AND 
                                                                   pic_file = '".$this->testGuideId.$lastInsertId."imagesnew.jpg'" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertTrue(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'soundnew.wav'));
        $this->assertTrue(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'imagesnew.jpg'));

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$result['video_file']);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);

        $this->assertTrue(strpos($data,"Ten film wideo nie został jeszcze przetworzony."));
        $this->assertTrue(strpos($data,"$this->testNameNew"));


    }

    public function testEditDeleteTextOnly()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );

        $this->assertTrue($this->setFieldByName('name', '' ));       
        $this->assertTrue($this->setFieldByName('desc', '' ));    
        $this->assertTrue($this->setFieldByName('geocode', '' ));    


        $this->click("Zapisz");

        $this->assertText("Podaj nazwę");
    }

    public function testEditDeletePic()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  pic_file = '".$this->testGuideId.$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_pic_file", 1 );
 
        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   pic_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertFalse(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg'));
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertNoText("Usuń zdjęcie");
    }

 
    public function testEditDeleteVideoCreatedinSystemAccount()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        
        //Upload video to YT
        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.avi");
        $myVideoEntry->setVideoDescription("video.avi");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.avi");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD'); 
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.avi",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);

        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE slide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        //////////////////////////////
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 1 );
        $this->assertText('Wideo w trakcie przetwarzania');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $this->assertTrue( $this->setFieldByName("yt_id", '' ) ); //clean yt_id

        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        
        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   video_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$matches[1]);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);
        //Get location 
        preg_match('/Location: (.*)\r\n/',$data,$matches);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $matches[1]."&hl=pl");
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

        $this->assertTrue(strpos($data,"Ten film wideo został usunięty przez użytkownika."));

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertNoText("Usuń video");        
    }
    


    public function testEditDeleteSound()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  sound_file = '".$this->testGuideId.$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_sound_file", 1 );
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->assertLink('Pobierz dźwięk',$global['service_url'].'send_sound.php?id='.$this->testGuideId.$lastInsertId.'sound.wav');
        $this->assertFalse( $this->setFieldByName("delete_video_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );



        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   sound_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertFalse(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav'));
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertNoText("Usuń dźwięk");
    }
    public function testEditDeletePicSoundVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();
        $dbh->exec("UPDATE slide SET  sound_file = '".$this->testGuideId.$lastInsertId."sound.wav' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/sound.wav', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav');


        //Upload video to YouTube

        $yt = Yt::getClient();
        Zend_Loader::loadClass('Zend_Gdata_YouTube');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        // create a new VideoEntry object
        $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

        $myVideoEntry->setVideoTitle("video.avi");
        $myVideoEntry->setVideoDescription("video.avi");
        $myVideoEntry->setVideoCategory('People');
        $myVideoEntry->SetVideoTags("video.avi");

        $tokenArray = $yt->getFormUploadToken($myVideoEntry);
        $tokenValue = $tokenArray['token'];
        $postUrl = $tokenArray['url'];

        // place to redirect user after upload
        $nextUrl = $global['service_url'];

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $postUrl .'?nexturl='. $nextUrl);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);//do not follow location
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);//no tunnel
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($global['proxy']) ? $global['proxy'] : null ); 
        curl_setopt($ch, CURLOPT_POST, true);
        $post = array(
            "file"=>"@".dirname(__FILE__)."/video.avi",
            "token"=>$tokenValue,
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

        $this->assertTrue(strpos($data, '?status=200' ));
 
        preg_match('/\&id=(.*)\r\n/',$data,$matches);

        $dbh->exec("UPDATE slide SET  video_file = '".$matches[1]."' WHERE rowid=".$lastInsertId);
        //////////////////////////

        $dbh->exec("UPDATE slide SET  pic_file = '".$this->testGuideId.$lastInsertId."images.jpg' WHERE rowid=".$lastInsertId);
        copy( dirname(__FILE__).'/images.jpg', $upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg');
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_sound_file", 1 );
        $this->setFieldByName("delete_video_file", 1 ) ;  
        $this->setFieldByName("delete_pic_file", 1 ) ;

        $this->click("Zapisz");
        $this->assertText("Slajd zaktualizowany.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   sound_file IS NULL AND
                                                                                     video_file IS NULL AND 
                                                                   pic_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        $this->assertFalse(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'images.jpg'));
        $this->assertFalse(file_exists($upload['upload_dir'].$this->testGuideId.$lastInsertId.'sound.wav'));

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, "http://www.youtube.com/watch?hl=pl&v=".$matches[1]);
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

       //Get location 
        preg_match('/Location: (.*)\r\n/',$data,$matches);

        $ch = curl_init();
         
        curl_setopt($ch, CURLOPT_URL, $matches[1]."&hl=pl");
        curl_setopt($ch, CURLOPT_HEADER,1);
        curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : null );
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0); 
        $options = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 
        curl_setopt($ch, CURLOPT_PROXY,  isset($options['proxy']) ? $options['proxy'] : null ); 
        $data = curl_exec($ch);
        curl_close($ch);
        //wait a while to avoid google too many calls error
        sleep(1);

        $this->assertTrue(strpos($data,"Ten film wideo został usunięty przez użytkownika."));

    }



   public function testFileSizeSet()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('MAX_FILE_SIZE',"2097152");
    }


    public function testRemoveSlide()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);

        $this->click("Usuń");

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');

        $this->click("Tak");

        $this->assertText("Slajd usunięty.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."'");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }

    public function testDoNotRemoveSlide()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);

        $this->click("Usuń");

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');
        
        $this->click("Nie");

        $this->assertField('name', $this->testName);

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."'");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testRemoveSlideAndCheckOrder()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 

        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','0','0','".$this->username."')");
        $lastInsertId[] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','1','1','".$this->username."')");   
        $lastInsertId[] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','2','2','".$this->username."')");
        $lastInsertId[] = $dbh->lastInsertId();

        $this->get($this->url.'?cmd=AdminAddSlide&id='. $lastInsertId[1].'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');

        $this->assertField('name', "1");

        $this->click("Usuń");

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');

        $this->click("Tak");

        $this->assertText("Slajd usunięty.");

        $sth = $dbh->prepare("SELECT `order` FROM slide WHERE name = '2' AND guide_id = ".$this->testGuideId);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $sth = $dbh->prepare("SELECT `order` FROM slide WHERE name = '0' AND guide_id = ".$this->testGuideId);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);

    }


    public function testOrderEditSlidesAndChangeOrder()
    {
 
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','0','0','".$this->username."')");
        $lastInsertId[] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','1','1','".$this->username."')");   
        $lastInsertId[] = $dbh->lastInsertId();
        $dbh->exec("INSERT INTO slide ( guide_id,name,`order`,owner ) VALUES ('".$this->testGuideId."','2','2','".$this->username."')");
        $lastInsertId[] = $dbh->lastInsertId();


        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId[0].'&guide_id='.$this->testGuideId);
        $this->assertField('order', 0); 
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId[1].'&guide_id='.$this->testGuideId);   
        $this->assertField('order',1);        
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId[2].'&guide_id='.$this->testGuideId);   
        $this->assertField('order', 2);
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId[1].'&guide_id='.$this->testGuideId);   
        $this->setField('order', 0);
        $this->click("Zapisz");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId[0].'&guide_id='.$this->testGuideId);   
        $this->setField('order', 2);
        $this->click("Zapisz");


        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');      
        $dbh = new PDO($options['dsn_string']);  
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        $sth = $dbh->prepare("SELECT name,`order` FROM slide WHERE guide_id = :guide_id ORDER BY `order` ASC");
        $sth->bindParam(':guide_id',$this->testGuideId,PDO::PARAM_INT);
        $sth->execute();
        $result = $sth->fetchAll(PDO::FETCH_BOTH);
        
        $row = $result['0'];
        $this->assertEqual($row['name'],1); 
        $this->assertEqual($row['order'],0); 

        $row = $result['1'];
        $this->assertEqual($row['name'],2); 
        $this->assertEqual($row['order'],1); 

        $row = $result['2'];
        $this->assertEqual($row['name'],0); 
        $this->assertEqual($row['order'],2); 

        
    }
 
    public function testShowLinkToAlreadyUploadedVideo()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."')");
        $lastInsertId = $dbh->lastInsertId();

        $dbh->exec("UPDATE slide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$lastInsertId);


        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 0 );
        // create a new VideoEntry object
        $yt = Yt::getClient();
        $videoEntry = new Zend_Gdata_YouTube_VideoEntry();
        $videoEntry = $yt->getVideoEntry('jNQXAC9IVRw');
        foreach ($videoEntry->mediaGroup->content as $content) {
            if ($content->type === "video/3gpp" && $content->format === '6') {
                $this->assertPattern('/rtsp.*video.3gp.*>Pobierz video<\/a>/');
            }
        }

        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );

        $dbh->exec("UPDATE slide SET  video_file = null WHERE rowid=".$lastInsertId);
    }

    public function testEditDeleteVideoNotFromAccount()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner,video_file ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."','jNQXAC9IVRw')");
        $lastInsertId = $dbh->lastInsertId();
        
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("delete_video_file", 1 );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );



        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        
        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   video_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertNoText("Usuń video");        
    }

    public function testEditDeleteVideoNotFromAccountEmptyYtId()
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN'); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
 
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );    
   
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner,video_file ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."','jNQXAC9IVRw')");
        $lastInsertId = $dbh->lastInsertId();
        
        
        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('geocode', $this->testGeocode);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertField('video_file');
        $this->setFieldByName("yt_id", '' );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 0 ) );  
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 0 ) );



        $this->click("Zapisz");

        $this->assertText("Slajd zaktualizowany.");

        
        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."' AND 
                                                                   desc = '".$this->testDesc."' AND 
                                                                   geocode = '".$this->testGeocode."' AND 
                                                                   guide_id = '".$this->testGuideId."' AND
                                                                   video_file IS NULL" );
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$lastInsertId.'&guide_id='.$this->testGuideId);
        $this->assertNoText("Usuń video");        
    }

    public function testRemoveSlideWithVideoNotFromAccount()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $dbh->exec("INSERT INTO slide ( guide_id,name, desc,geocode,owner,video_file ) VALUES ('".$this->testGuideId."','".$this->testName."','".$this->testDesc."','".$this->testGeocode."','".$this->username."','jNQXAC9IVRw')");

        $this->get($this->url.'?cmd=AdminAddSlide&id='.$dbh->lastInsertId().'&guide_id='.$this->testGuideId);

        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);

        $this->click("Usuń");

        $this->assertField('Submit', 'Tak');
        $this->assertField('cancel', 'Nie');

        $this->click("Tak");

        $this->assertText("Slajd usunięty.");

        $sth = $dbh->prepare("SELECT COUNT(ROWID) FROM slide WHERE name = '".$this->testName."'");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }



    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $sth = $dbh->query("SELECT pic_file,sound_file,video_file FROM slide WHERE guide_id=".$this->testGuideId);

        $result = $sth->fetch();
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }

        if ( is_file($upload['upload_dir'].$result['sound_file']) )
        {
            unlink($upload['upload_dir'].$result['sound_file']);
        }


        if ( $result['video_file'] != '' )
        {
            $yt = Yt::getClient();
            try
            {
                $videoEntry = $yt->getVideoEntry($result['video_file'],null,true);
                $yt->delete($videoEntry);
            }
            catch(Zend_Gdata_App_HttpException $e)
            {
            }
        }

        $sth = $dbh->prepare("DELETE FROM slide WHERE guide_id=".$this->testGuideId);
        $sth -> execute();
        $sth = $dbh->prepare("DELETE FROM guide WHERE ROWID=".$this->testGuideId);
        $sth -> execute();
        $dbh = null;

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
    }
    
}
?>
